package com.demo.server.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.server.entity.resp.CaptchaResp;
import com.demo.server.util.MyCacheUtil;
import com.demo.server.util.ResultData;
import com.demo.server.util.UUIDUtil;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * @date 2023-10-19
 * @author wulianwei
 */
@Slf4j
@RestController
@RequestMapping("public")
@Tag(name="公共接口")
public class PublicController {
	
	@PostMapping("getCaptcha")
    @Operation(summary = "获取验证码")
	public ResultData<CaptchaResp> getCaptcha(){
		log.info("getCaptcha>>");
		String captchaKey = UUIDUtil.get32UUID();
		ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 4, 0);
		captcha.setGenerator(new MathGenerator(1));
		captcha.createCode();
		MyCacheUtil.captchaCache.put(captchaKey, captcha.getCode());
		String base64String = "data:image/png;base64,"+captcha.getImageBase64();
		CaptchaResp resp = new CaptchaResp();
		resp.setCaptchaKey(captchaKey);
		resp.setCaptchaImg(base64String);
		return ResultData.success(resp);
	}
}
